<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.blurry" clearable size="small" placeholder="模糊搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">项目类别</label>
        <el-input v-model="query.bisYmkdzXmlb" clearable placeholder="项目类别" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="840px">
        <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
          <el-input v-model="form.bisYmkdzYmkId" type="hidden" readonly style="width: 150px;" />
          <el-row>
            <el-col :span="8">
              <el-form-item label="编码" prop="bisYmkdzYbbm">
                <el-input v-model="form.bisYmkdzYbbm" readonly :disabled="true" style="width: 150px;">
                  <el-button slot="append" icon="el-icon-search" title="打开医保药品目录" @click="getYmk" />
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="名称" prop="bisYmkdzYbmc">
                <el-input v-model="form.bisYmkdzYbmc" style="width: 150px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="助记码" prop="bisYmkdzYbzjm">
                <el-input v-model="form.bisYmkdzYbzjm" style="width: 150px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="价格" prop="bisYmkdzDj">
                <el-input-number v-model="form.bisYmkdzDj" :precision="2" :step="0.01" style="width: 150px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="服务时长" prop="bisYmkdzYbfwsc">
                <el-input v-model="form.bisYmkdzYbfwsc" style="width: 150px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider />
          <el-row>
            <el-col :span="8">
              <el-form-item label="剂型" prop="bisYmkdzJx">
                <el-input v-model="form.bisYmkdzJx" style="width: 150px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="剂量" prop="bisYmkdzJl">
                <el-input-number v-model="form.bisYmkdzJl" :precision="2" :step="1" style="width: 150px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="使用频次" prop="bisYmkdzSypc">
                <el-input v-model="form.bisYmkdzSypc" style="width: 150px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="用法" prop="bisYmkdzYf">
                <el-input v-model="form.bisYmkdzYf" style="width: 150px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="用量" prop="bisYmkdzYl">
                <el-input-number v-model="form.bisYmkdzYl" :precision="2" :step="1" style="width: 150px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="执行天数" prop="bisYmkdzZxts">
                <el-input-number v-model="form.bisYmkdzZxts" :min="1" :max="1000" style="width: 150px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="单位" prop="bisYmkdzDw">
                <el-input v-model="form.bisYmkdzDw" style="width: 150px;" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="规格" prop="bisYmkdzGg">
                <el-input v-model="form.bisYmkdzGg" style="width: 150px;" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="bisYmkdzYbbm" label="编码" />
        <el-table-column prop="bisYmkdzYbmc" label="名称" />
        <el-table-column prop="bisYmkdzYbzjm" label="助记码" />
        <el-table-column prop="bisYmkdzYbfwsc" label="服务时长" />
        <el-table-column prop="bisYmkdzDj" label="单价" />
        <el-table-column prop="bisYmkdzJslx" label="结算类型">
          <template slot-scope="scope">
            {{ dict.label.sfxmdj[scope.row.bisYmkdzJslx] }}
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','ymkdz:edit','ymkdz:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <!--Drawer 抽屉，隐藏异名库列表-->
      <el-drawer
        title="基础异名库"
        :visible.sync="table"
        direction="rtl"
        size="50%"
      >
        <el-table ref="tableDrawer" v-loading="crud.loading" :data="ymkData" height="100%">
          <el-table-column type="index" :index="indexMethod" label="序号" align="center" />
          <el-table-column prop="aka090" label="医保对照吗" align="center" />
          <el-table-column prop="aka091" label="医保名称" align="center" />
          <el-table-column prop="aka066" label="医保助记码" align="center" />
          <el-table-column prop="aka068" label="医保限价" align="center" />
          <el-table-column prop="cka003" label="医保服务时长" align="center" />
          <el-table-column prop="aka065" label="医保服务类别" align="center">
            <template slot-scope="scope">
              {{ dict.label.sfxmdj[scope.row.aka065] }}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="120"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                @click.native.prevent="selectRow(scope.$index, ymkData)"
              >
                确定
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-drawer>
    </div>
  </div>
</template>

<script>
import crudYmkdz from '@/api/business/ymkdz'
import { getYmk } from '@/api/business/ymk'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { bisYmkdzId: null, bisYmkdzYmkId: null, bisYmkdzHljgId: null, bisYmkdzMc: null, bisYmkdzZjm: null, bisYmkdzDj: null, bisYmkdzFwsc: null, bisYmkdzXmlb: null, bisYmkdzYbbm: null, bisYmkdzYbmc: null, bisYmkdzYbzjm: null, bisYmkdzYbfwsc: null, bisYmkdzZxts: null, bisYmkdzJx: null, bisYmkdzJl: null, bisYmkdzYf: null, bisYmkdzYl: null, bisYmkdzSypc: null, bisYmkdzDw: null, bisYmkdzGg: null, ywlx: null, lrr: null, lrrxm: null, lrbm: null, lrbmmc: null, lrsj: null, gxr: null, gxrxm: null, gxbm: null, gxbmmc: null, gxsj: null, sfyx: null, bz: null }
export default {
  name: 'Ymkdz',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['sfxmdj'],
  cruds() {
    return CRUD({ title: '异名库对照', url: 'api/ymkdz', idField: 'bisYmkdzId', sort: 'bisYmkdzYbbm,asc', crudMethod: { ...crudYmkdz }})
  },
  data() {
    return {
      table: false,
      ymkData: [],
      permission: {
        add: ['admin', 'ymkdz:add'],
        edit: ['admin', 'ymkdz:edit'],
        del: ['admin', 'ymkdz:del']
      },
      rules: {
        bisYmkdzMc: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        bisYmkdzZjm: [
          { required: true, message: '助记码不能为空', trigger: 'blur' }
        ],
        bisYmkdzDj: [
          { required: true, message: '单价不能为空', trigger: 'blur' }
        ],
        bisYmkdzFwsc: [
          { required: true, message: '服务时长不能为空', trigger: 'blur' }
        ],
        bisYmkdzXmlb: [
          { required: true, message: '项目类别不能为空', trigger: 'blur' }
        ],
        bisYmkdzYbbm: [
          { required: true, message: '医保编码不能为空', trigger: 'blur' }
        ],
        bisYmkdzYbmc: [
          { required: true, message: '医保名称不能为空', trigger: 'blur' }
        ],
        bisYmkdzYbzjm: [
          { required: true, message: '医保助记码不能为空', trigger: 'blur' }
        ],
        bisYmkdzZxts: [
          { required: true, message: '执行天数不能为空', trigger: 'blur' }
        ],
        bisYmkdzJx: [
          { required: true, message: '剂型不能为空', trigger: 'blur' }
        ],
        bisYmkdzJl: [
          { required: true, message: '剂量不能为空', trigger: 'blur' }
        ],
        bisYmkdzYf: [
          { required: true, message: '用法不能为空', trigger: 'blur' }
        ],
        bisYmkdzYl: [
          { required: true, message: '用量不能为空', trigger: 'blur' }
        ],
        bisYmkdzSypc: [
          { required: true, message: '使用频次不能为空', trigger: 'blur' }
        ],
        bisYmkdzDw: [
          { required: true, message: '单位不能为空', trigger: 'blur' }
        ],
        bisYmkdzGg: [
          { required: true, message: '规格不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'bisYmkdzXmlb', display_name: '项目类别' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    /**
     * 异名库的序号
     * @param index
     * @returns {*}
     */
    indexMethod(index) {
      return index + 1
    },
    /**
     * 确认选择的项目
     * @param index
     * @param rows
     */
    selectRow(index, rows) {
      // console.log(rows[index])
      const ymk = rows[index]
      console.log(ymk)
      console.log(ymk.aka090.indexOf('JJZH'))
      if (ymk.aka090.indexOf('JJZH') !== -1) {
        this.$notify({
          title: '禁止选择居家类服务项目',
          type: 'warning',
          duration: 2500
        })
        return false
      }
      /*
      let flag = false
      this.crud.data.forEach(data => {
        console.log(data.bisYmkdzYbbm)
        console.log(ymk.bisYmkYbdzm)
        const temp_bisYmkdzYbbm = data.bisYmkdzYbbm
        if (temp_bisYmkdzYbbm === ymk.bisYmkYbdzm) {
          flag = true
        }
      })
      if (flag) {
        this.$notify({
          title: '名称[' + ymk.bisYmkYbmc + ']已经存在',
          type: 'warning',
          duration: 2500
        })
        return false
      }
      */

      this.table = false
      this.form.bisYmkdzMc = ymk.aka091
      this.form.bisYmkdzZjm = ymk.aka066
      this.form.bisYmkdzYbbm = ymk.aka090
      this.form.bisYmkdzYbmc = ymk.aka091
      this.form.bisYmkdzYbzjm = ymk.aka066
      this.form.bisYmkdzYbfwsc = ymk.cka003
      this.form.bisYmkdzFwsc = ymk.cka003
      this.form.bisYmkdzDj = ymk.aka068
      this.form.bisYmkdzYbdj = ymk.aka068
      this.form.bisYmkdzJslx = ymk.aka065
      this.form.bisYmkdzFwlx = ymk.yllb
    },
    /**
     * 打开异名库时，实时查询
     */
    getYmk() {
      this.table = true
      if (this.ymkData.length === 0) {
        getYmk({ enabled: true }).then(res => {
          console.log(res)
          this.ymkData = res
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
